#!/bin/sh
source /root/.bash_profile

program_path=GameServer

DAEMON="daemon"
ENGINE="engine"
CHAT="chat"
GATE="gate"
SANDBOX="sandbox"
DBSERVER="dbserver"
DIRCSERVER="dircserver"
LOG="log"

usage()
{
	echo "usage: update | start | core | ntp"
}

update()
{
	PID_DAEMON=`pidof daemon`
	if [ "$PID_DAEMON" != "" ];then
		echo "daemon proc exist..."
		exit
	fi

	if [ ! -f $program_path ];then
		echo "can't find $program_path"
		exit
	fi

	rm -rf $DAEMON
	rm -rf $ENGINE
	rm -rf $CHAT
	rm -rf $GATE
	rm -rf $SANDBOX
	rm -rf $DBSERVER
	rm -rf $DIRCSERVER

	mkdir -p $DAEMON
	mkdir -p $ENGINE
	mkdir -p $CHAT
	mkdir -p $GATE
	mkdir -p $SANDBOX
	mkdir -p $DBSERVER
	mkdir -p $DIRCSERVER
	mkdir -p $LOG

	cp  $program_path $DAEMON/$DAEMON
	cp  $program_path $ENGINE/$ENGINE
	cp  $program_path $CHAT/$CHAT
	cp  $program_path $GATE/$GATE
	cp  $program_path $SANDBOX/$SANDBOX
	cp  $program_path $DBSERVER/$DBSERVER
	cp  $program_path $DIRCSERVER/$DIRCSERVER

	core
	start
}

core()
{
#################set coredown file##################
# %p: process id
# %s: core dump signal
# %h: hostname
# %e: program name
##################################################
	echo "set coredown file format: core.program_name.pid"
	ulimit -c unlimited
	ulimit -n 65534
	echo 'core.%e.%p' > /proc/sys/kernel/core_pattern
}

ntp()
{
	/usr/sbin/ntpdate time.windows.com 210.72.145.44
}


#check if there is a service running

start()
{
	PID_DAEMON=`pidof daemon`
	if [ "$PID_DAEMON" != "" ];then
		echo "daemon proc exist..."
		exit
	fi
	
	echo "===start daemon===="
	./$DAEMON/$DAEMON poker >/dev/null 2>&1  &
}

case "$1" in
	update)
		update;;
	start)
		start;;
	core)
		core;;
	ntp)
		ntp;;
	*)
		usage;;
esac
